function RegisterController($scope,$state,Notification,FileUploader,RegisterService,ErrorUtils,CONSTANTS,context,$cookies,loadContext){
	Notification.success({message: 'Please fill the form for new registration.', title: 'Welcome to CleanEstimateX System'});
	
	$scope.registerText = "Register";
	$scope.showLoading  = false;
	$scope.companyTradeLicense = "";
	
	var uploader = $scope.uploader = new FileUploader({
        url: CONSTANTS.BASE_REST_URL+'/rest/public/uploadfile'
    });
	
	$scope.register = function(){
				
		if($scope.uploader.queue == undefined || $scope.uploader.queue.length<1){
			Notification.error({message: 'Please select company trade license.', title: 'Error'});
		}		
		else if($scope.companyType.value == 0){
			Notification.error({message: 'Please select company type.', title: 'Error'});
		}
		else{
			//Upload the document.
			try{
				uploader.queue[0].upload();
			}
			catch(err){
				console.log("Uploading Error: "+err.message);
			}
			
			var registerObject = 	"companyName="+$scope.companyName+"&"+
									"companyOverview="+"&"+
									"companyType="+$scope.companyType.name+"&"+
									"companyWebLink="+$scope.companyWebsite+"&"+
									"companyLicenseNumber="+"&"+
									"companyLicenseLink="+uploader.queue[0].file.name+"&"+
									"companyAddress="+"&"+
									"companyCountry=United Arab Emirates"+"&"+
									"employeeFirstName="+$scope.firstName+"&"+
									"employeeLastName="+$scope.lastName+"&"+
									"employeeEmail="+$scope.email+"&"+
									"employeePassword="+$scope.password+"&"+
									"employeePhone="+$scope.phone;
			
			
			$scope.toggleLoading();
			var save = RegisterService.save(registerObject);
			save.$promise.then(
					function(data){
						$scope.toggleLoading();
						if(data.meta.code == 200){
							Notification.success({message: 'Registered successfully. Please check your email for confirmation.', title: 'Success'});
							context.setEmployee(data.data);
							
							//Store employee in session storage
							sessionStorage.setItem('employee',JSON.stringify(context.getEmployee()) );
							
							loadContext.redirectToHomePage();
						}
						else{
							Notification.error({message:ErrorUtils.getMessageByErrorCode(data.meta.code), title: 'Error'});
						}
						
					},
					function(error){
						$scope.toggleLoading();
						Notification.error({message: "Some error occurred. Please try again later.", title: 'Error'});
					});
		}
		
		
		
	}
	
	$scope.toggleLoading = function(){
		if($scope.registerText == "Register"){
			$scope.registerText = "Signing Up. Please wait..";
			$scope.showLoading = true;
		}
		else{
			$scope.registerText = "Register";
			$scope.showLoading = false;
		}
	}
	
	
	$scope.companyTypes = [
	                      	{name:'Company Type',value:0},
	                      	{name:'Building Owner',value:1},
	                      	{name:'Contractor',value:2},
	                      	{name:'Supplier',value:3}
	                      ];
	//Default selection
	$scope.companyType = $scope.companyTypes[0];
	
	
	

    // FILTERS

    uploader.filters.push({
        name: 'customFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            return this.queue.length < 10;
        }
    });

    // CALLBACKS

    uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/, filter, options) {
        console.info('onWhenAddingFileFailed', item, filter, options);
    };
    uploader.onAfterAddingFile = function(fileItem) {
    	if(this.queue.length>1){
    		uploader.queue[0] = fileItem;
    		uploader.queue[1].remove();
    	}
    	
    };
    uploader.onAfterAddingAll = function(addedFileItems) {
        console.info('onAfterAddingAll', addedFileItems);
    };
    uploader.onBeforeUploadItem = function(item) {
        console.info('onBeforeUploadItem', item);
    };
    uploader.onProgressItem = function(fileItem, progress) {
        console.info('onProgressItem', fileItem, progress);
    };
    uploader.onProgressAll = function(progress) {
        console.info('onProgressAll', progress);
    };
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        console.info('onSuccessItem', fileItem, response, status, headers);
    };
    uploader.onErrorItem = function(fileItem, response, status, headers) {
        console.info('onErrorItem', fileItem, response, status, headers);
    };
    uploader.onCancelItem = function(fileItem, response, status, headers) {
        console.info('onCancelItem', fileItem, response, status, headers);
    };
    uploader.onCompleteItem = function(fileItem, response, status, headers) {
        console.info('onCompleteItem', fileItem, response, status, headers);
    };
    uploader.onCompleteAll = function() {
        console.info('onCompleteAll');
    };
    
    
	
		
}
angular.module('register',['ngAnimate','ui.router','ui-notification','angularFileUpload'])
	.controller('RegisterController',['$scope','$state','Notification','FileUploader','RegisterService','ErrorUtils','CONSTANTS','context','$cookies','loadContext',RegisterController]);